﻿using System.Collections.Generic;
using System.Linq;

using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal class FixtureDataProxy : Business.Tournaments.Fixture
	{
		private IDataProxy<SqlServer.Tournament> _dataProxy;

		internal FixtureDataProxy(Business.Tournaments.Tournament tournament, IDataProxy<SqlServer.Tournament> dataProxy)
			: base(tournament)
		{
			this._dataProxy = dataProxy;
			this.Journeys = null; //TODO: esto es bastante fulero!!!
		}

		#region Override properties

		internal bool JourneysHasBeenRead { get; set; }
		protected override List<Business.Tournaments.Journey> Journeys
		{
			get
			{
				if (!JourneysHasBeenRead)
				{
					JourneysHasBeenRead = true;
					if (base.Journeys == null)
					{
						base.Journeys = MapJourneys();
					}
				}
				return base.Journeys;
			}
		}
		internal List<Business.Tournaments.Journey> InternalJourneys
		{
			get { return this.Journeys; }
		}

		#endregion

		#region Mapping methods

		private List<Business.Tournaments.Journey> MapJourneys()
		{
			return _dataProxy.InnerDataObject.Journeys.Select(journey => JourneysDataMapper.Instance.DataObject2BusinessObject(journey)).ToList();
		}

		#endregion
	}
}
